<template>
	<view>
		<view class="content-login">
			<view class="tabbar">
				<uni-icons class="left-icon" backgroundColor="#FFFFFF" color="#000" type="back"
					@click="back()"></uni-icons>
				<view class="title" style="color: #000;">个人注册</view>
			</view>
			<view class="top-title-view">
			</view>
			<scroll-view class="select-facility-view regform" scroll-y="true"
				:style="{height:setcollapseheight() + 'px'}">
				<uni-forms border ref="form" v-model="formdata" :rules="rules">
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/card-type.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							证件类型:
						</view>

						<uni-forms-item name="documentType">
							<picker mode=selector @change="selectCardType" range-key="label" :range="cardTypeList"
								class="picker" :class="{ 'nodata': cardTypeName == '请选择运证件类型' }">
								{{ cardTypeName }}
							</picker>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/user.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							姓名:
						</view>

						<uni-forms-item name="name">
							<uni-easyinput :inputBorder="false" v-model="formdata.name" placeholder="请输入姓名"
								trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/card-type.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							证件号码:
						</view>

						<uni-forms-item name="idNumber">
							<uni-easyinput :inputBorder="false" v-model="formdata.idNumber" placeholder="请输入证件号码"
								trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/date.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							证件有效期:
						</view>

						<uni-forms-item name="cardDate" class="dateRange">
							<uni-datetime-picker v-model="formdata.cardDate" @change="cardDateChange" type="datetimerange" :border="false" rangeSeparator="-" />
						</uni-forms-item>
					</view>
					<view class="tip">
						<view class="desc">
							请选择证件有效期，如与实体证件不符请手动调整
						</view>
						<uni-forms-item name="validity" class="validity">
							<u-radio-group placement="row" size="32" v-model="formdata.validity">
								<u-radio labelSize="24" iconSize="24" :customStyle="{marginRight:'8px'}"
									v-for="(item, index) in checkboxList" :key="index" :label="item.label"
									:name="item.value" shape="square"></u-radio>
							</u-radio-group>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/phone.png"
								mode="heightFix"></image>
						</view>
						<view class="content-view-item-value">
							手机号:
						</view>
						<uni-forms-item name="phone">
							<uni-easyinput :inputBorder="false" v-model="formdata.phone" placeholder="请输入手机号"
								trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/emiyanzhengma.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							验证码:
						</view>

						<uni-forms-item name="smsCode" class="code-box">
							<uni-easyinput :inputBorder="false" v-model="formdata.smsCode" placeholder="请输入验证码" type="number"
								trim="all"></uni-easyinput>
						</uni-forms-item>
						
						<view class="code-btn" @click="getTextCode">
							{{ captchaText }}
						</view>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/email.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							邮箱:
						</view>

						<uni-forms-item>
							<uni-easyinput :inputBorder="false" v-model="formdata.email" placeholder="请输入邮箱(选填)"
								trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/address.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							地址:
						</view>

						<uni-forms-item>
							<uni-easyinput :inputBorder="false" v-model="formdata.address" placeholder="请输入地址(选填)"
								trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/user.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							账号:
						</view>

						<uni-forms-item name="username">
							<uni-easyinput :inputBorder="false" v-model="formdata.username" placeholder="请输入账号"
								trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/mima-2.png"
								mode="heightFix"></image>
						</view>
						<view class="content-view-item-value">
							密码:
						</view>
						<uni-forms-item name="password">
							<!-- <input type="password" style='width:0;height:0;min-height:0'/>
							<input type="text" autocomplete="off" style='width:0;height:0;min-height:0'/> -->
							<uni-easyinput :inputBorder="false" v-model="formdata.password" placeholder="请设置8-20位登陆密码"
								type="password" trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/mima-2.png"
								mode="heightFix"></image>
						</view>
						<view class="content-view-item-value">
							确认密码:
						</view>
						<uni-forms-item name="Tpassword">
							<uni-easyinput :inputBorder="false" v-model="formdata.Tpassword" placeholder="请再次确认登录密码"
								type="password" trim="all"></uni-easyinput>
						</uni-forms-item>
					</view>
					<!-- <view class="regform-input">
						<view class="regform-input-view">
							<image class="regform-input-img" src="@/static/socialized-service/register/emiyanzhengma.png"
								mode="heightFix"></image>
						</view>
						
						<view class="content-view-item-value">
							运营方:
						</view>

						<uni-forms-item name="serviceProviderNo">
							<picker mode=selector @change="selectOperate" range-key="name" :range="operatorList"
								class="picker" :class="{ 'nodata': operateName == '请选择运营方' }">
								{{ operateName }}
							</picker>
						</uni-forms-item>
					</view> -->
				</uni-forms>
			</scroll-view>
		</view>
		<view class="button-buttom">
			<view class="chooseconfig">
				<label @click="agreement">
					<checkbox />
					同意
					<text style="color: #0288ED;">《用户服务协议》</text>
				</label>
			</view>
			<button @click="register">注册</button>
			<text class="light" @click="gotoSelogin">已有账户，立即登录</text>
		</view>
	</view>
</template>

<script>
	import agrApi from '@/api/socialized-servce/agricultural-information/index.js'
	import LoginApi from '@/api/socialized-servce/login/index.js'
	import agriculturalApi from '@/api/socialized-servce/agricultural-information/index.js'
	export default {
		data() {

			return {
				isshowcount: true, //发送短信按钮和倒计时切换
				formdata: {
					documentType: '1',
					name: '',
					phone: '',
					smsCode: '',
					password: '',
					Tpassword: '',
					serviceProviderNo: '',
				},
				cardTypeList: [
					{ label: '中华人民共和国居民身份证', value: '1' }
				],
				checkboxList: [
					{ label: '5年', value: 5 },
					{ label: '10年', value: 10 },
					{ label: '20年', value: 20 },
					{ label: '不限', value: '不限' },
				],
				disabled: false,
				cardTypeName: '中华人民共和国居民身份证',
				captchaText: '获取验证码',
				checkAgreement: false,
				operateName: '请选择运营方',
				operatorList: [],
				rules: {
					documentType:  {
						rules: [
							{
								required: true,
								errorMessage: '请选择证件类型',
							},
						],
					},
					idNumber:  {
						rules: [
							{
								required: true,
								errorMessage: '请输入证件号码',
							},
							{
								pattern: /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
								errorMessage: "请输入合法的证件号码",
							}
						],
					},
					cardDate:  {
						rules: [
							{
								required: true,
								errorMessage: '请选择证件有效期',
							},
						],
					},
					validity:  {
						rules: [
							{
								required: true,
								errorMessage: '请选择有效期',
							},
						],
					},
					phone: {
						rules: [{
								required: true,
								errorMessage: '请填写手机号',
							},
							{
								minLength: 11,
								maxLength: 11,
								errorMessage: '请输入正确的手机号',
							}
						],
					},
					smsCode: {
						rules: [{
								required: true,
								errorMessage: '请输入验证码',
							},
						],
					},
					name: {
						rules: [{
								required: true,
								errorMessage: '请输入姓名',
							},
						],
					},
					username: {
						rules: [{
								required: true,
								errorMessage: '请输入账号',
							},
						],
					},
					password: {
						rules: [
							{
								required: true,
								errorMessage: '两次密码不一致',
							},
							{
								pattern: /^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*.]{8,20}$/,
								errorMessage: '密码必须是8到20位的数字和字母组合',
							},
						]
					},
					Tpassword: {
						rules: [
							{
								required: true,
								errorMessage: '两次密码不一致',
							},
							{
								pattern: /^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*.]{8,20}$/,
								errorMessage: '密码必须是8到20位的数字和字母组合',
							},
						]
					}
				}
			}
		},
		onReady() {
			// this.getOperatorList()
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			cardDateChange () {
				let startTime = new Date(this.formdata.cardDate[0]).getTime(), endTime = new Date(this.formdata.cardDate[1]).getTime()
				let time = (endTime - startTime) / (365 * 24 * 60 * 60 * 1000)
				if (time <= 6) {
					this.$set(this.formdata, 'validity', 5)
				} else if (time <= 12) {
					this.$set(this.formdata, 'validity', 10)
				} else if (time <= 21) {
					this.$set(this.formdata, 'validity', 20)
				} else {
					this.$set(this.formdata, 'validity', 'all')
				}
			},
			//发送验证码
			getTextCode() {
				var that = this;
				if (that.disabled) return
				const myreg = /^1[3456789]\d{9}$/;
				// var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
				var dataMobile = this.formdata.phone;
				if (dataMobile == '') {
					uni.showToast({
						title: '请输入您的联系方式',
						duration: 2000,
						icon: 'none'
					});
					return false;
				}
				if (!myreg.test(dataMobile)) {
					uni.showToast({
						title: '联系方式格式不正确',
						duration: 2000,
						icon: 'none'
					});
					return false;
				}
				LoginApi.queryVerificationCode({
					phone: dataMobile,
					signName: '美农'
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '发送成功',
							duration: 2000,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						});
					}
				})
				// 倒计时时间 60秒
				that.time = 60;
				that.fn = setInterval(function() {
					that.time--
					that.captchaText = that.time + 's';
					that.disabled = true;
					if (that.time == 0) {
						that.fn && clearInterval(that.fn)
						that.disabled = false;
						that.captchaText = '获取验证码';
					}
				}, 1000);
			},
			// 普通用户获取运营方
			async getOperatorList() {
				let params = {
					status: 0,
					longitude: uni.getStorageSync('lng'),
					latitude: uni.getStorageSync('lat')
				}
				let res = await agriculturalApi.queryOperator(params)
				if(res.code === 200) {
					this.operatorList = res.data
					console.log(this.operatorList, 123)
				}
			},
			selectCardType(e) {
				this.cardTypeName = this.cardTypeList[e.detail.value || 0].label
				this.$set(this.formdata, 'documentType', this.cardTypeList[e.detail.value || 0].value)
			},
			selectOperate(e) {
				this.operateName = this.operatorList[e.detail.value || 0].name
				this.formdata.serviceProviderNo = this.operatorList[e.detail.value || 0].serviceProviderNo
			},
			agreement() {
				this.checkAgreement = !this.checkAgreement;
			},
			// ====发送验证码====
			sendCaptcha() {
				// console.log('11');
				this.$refs.countDown.start();
				this.isshowcount = false
			},
			finishcount() {
				this.$refs.countDown.reset();
				this.isshowcount = true
			},
			// ====注册====
			register() {
				this.$refs.form.validate().then(res => {
					if (this.formdata.password !== this.formdata.Tpassword) {
						uni.showToast({
							title: `两次密码不一致！！！`,
							icon: 'none'
						})
						return
					}
					if (!this.checkAgreement) {
						uni.showToast({
							title: `请同意《用户服务协议》`,
							icon: 'none'
						})
						return
					}
					let param = {
						...this.formdata,
						usertype: 0,
						phoneNumber: this.formdata.phone,
						identifyingCode: this.formdata.smsCode,
						startTime: this.formdata.cardDate[0],
						endTime: this.formdata.cardDate[1],
						credential: {
							smsCode: this.formdata.smsCode
						},
					}
					LoginApi.registerSso(param).then(res => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						if (res.code == 200) {
							setTimeout(() => {
								const url =
									`/pages/login/login?username=${this.formdata.phone}&password=${this.formdata.password}`
								this.$togo(url)
							}, 1000);
						}
					})

				}).catch(err => {
					console.log('err', err);
				})
			},
			// 子系统注册
			registerTwo(param) {
				LoginApi.register(param).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
					if (res.code == 200) {
						setTimeout(() => {
							const url =
								`/pages/login/login?username=${this.formdata.phone}&password=${this.formdata.password}`
							this.$togo(url)
						}, 1000);
					}
				})
			},
			//去登录界面
			gotoSelogin() {
				const url = '/pages/login/login'
				this.$togo(url)
			},

			//标题栏返回上一层
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			//  设置可选择设施高度
			setcollapseheight() {
				let winHeight = 0
				uni.getSystemInfo({
					success: function(res) {
						winHeight = res.windowHeight
					}
				})
				let oldHeight = uni.upx2px(430)
				winHeight = parseInt(winHeight) - oldHeight
				// #ifndef H5
				winHeight = winHeight - 10
				// #endif
				return winHeight
			},
		}

	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-checkbox .uni-checkbox-input {
		width: 18rpx;
		height: 18rpx;
	}

	::v-deep .uni-easyinput__content-input[data-v-abe12412] {
		width: 500rpx;
	}
	
	::v-deep .uni-forms-item__content {
		width: 100%;
	}

	.uni-forms-item {
		position: relative;
		border: none !important;
		width: calc(100% - 210rpx);

		.picker {
			position: absolute;
			width: 100%;
			top: 50%;
			transform: translateY(-50%);
			padding-left: 20rpx;
			margin-right: 30rpx;

			.u-input {
				width: 500rpx;
				font-size: 28rpx !important;
			}

			.uni-input-placeholder {
				font-size: 28rpx;
				color: gray !important;
			}

			/deep/.u-input__content__field-wrapper__field {
				font-size: 28rpx !important;
			}
			
			&.nodata {
				color: grey;
			}
		}

		.uniIcon {
			position: absolute;
			right: 30rpx;
			top: 50%;
			transform: translateY(-50%);
		}
		
		&.code-box {
			width: calc(100% - 390rpx);
		}
	}
	
	.code-btn {
		font-size: 26rpx;
		padding: 6rpx 12rpx;
		background: #e6dce0;
		color: #0288ed;
		border-radius: 10rpx;
		margin-left: 12rpx;
		width: 160rpx;
		text-align: center;
	}

	.button-buttom {
		width: 100%;
		position: fixed;
		bottom: 40rpx;
		left: 50%;
		transform: translateX(-50%);
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-direction: column;

		.chooseconfig {
			position: absolute;
			left: 30rpx;
			top: -60rpx;
		}

		button {
			width: 472rpx;
			height: 64rpx;
			line-height: 64rpx;
			background: #0288ED;
			box-shadow: 0rpx 0rpx 14rpx 2rpx #0288ED;
			border-radius: 20rpx;
			font-size: 28rpx;
			font-weight: 400;
			color: #FFFFFF;
		}

		.light {
			margin-top: 22rpx;
			height: 28rpx;
			font-size: 28rpx;
			color: #0288ED;
		}
	}

	.content-login {
		width: 100%;
		background-color: #FFFFFF;
		position: relative;



		.tabbar {
			width: 750rpx;
			// height: 454rpx;
			color: #393939;
			display: flex;
			justify-content: center;
			position: fixed;
			z-index: 10;
			padding-bottom: 26rpx;
			border-bottom: 1px solid #e9eaee;

			.title {
				// width: 750rpx;
				margin-top: 90rpx;
				height: 32rpx;
				line-height: 32rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #FFFFFF;
			}

			.left-icon {
				position: fixed;
				top: 90rpx;
				left: 30rpx;
			}
		}

		.regform {
			position: fixed;
			top: 200rpx;
			left: 50%;
			transform: translateX(-50%);

			.regform-input {
				width: calc(100% - 60rpx);
				margin: 0 30rpx 45rpx;
				height: 80rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 12rpx 2rpx rgba(0, 0, 0, 0.05);
				border-radius: 16rpx;
				opacity: 1;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				position: relative;

				&:last-child {
					margin-bottom: 0;
				}

				.sendcaptcha {
					position: absolute;
					right: 22rpx;
					width: 160rpx;
					height: 32rpx;
					line-height: 32rpx;
					font-size: 32rpx;
					color: #0288ED;
					border-bottom: 1rpx solid #0288ED;
				}

				.regform-count {
					position: absolute;
					right: 22rpx;
					width: 80rpx;
					height: 32rpx;
					line-height: 32rpx;
					font-size: 32rpx;
					color: #0288ED;
				}


				.regform-input-view {
					width: 54rpx;
					height: 54rpx;
					background: #A2D7FD;
					border-radius: 50%;
					opacity: 1;
					margin-left: 22rpx;
					// margin: 0px 12rpx 0rpx 22rpx;
					display: flex;
					justify-content: center;
					// align-content: center;
				}
				
				.content-view-item-value {
					width: 120rpx;
					font-size: 26rpx;
					padding-left: 20rpx;
				}

				.regform-input-img {
					width: 36rpx;
					height: 36rpx;
					margin-top: 10rpx;


				}
			}
		}


		.top-title-view {
			// position: relative;
			// width: 100%;
			// height: 400rpx;
			// background: linear-gradient(180deg, #0480FB 0%, #0480FB 100%);
			// padding-top: 80rpx;

			.top-title-view-fish {

				// position: relative;
				// width: 100%;
				// height: 100%;
				.fish-view {

					// height: 100rpx;
					.bigfish {
						position: absolute;
						top: 100rpx;
						height: 120rpx;
						width: 250rpx;
					}

					.smallfish {
						position: absolute;
						top: 200rpx;
						right: 0;
						height: 100rpx;
						width: 150rpx;
					}
				}
			}

			.title-view-center {
				width: 100%;
				margin-top: 120rpx;
				height: 130rpx;
				display: flex;
				flex-wrap: wrap;
				justify-content: center;

				.title-img {
					height: 64rpx;
					width: 258rpx;
				}

				.title-name {
					height: 50rpx;
					width: 100%;
					text-align: center;
					height: 50rpx;
					line-height: 50rpx;
					color: #FFFFFF;
					font-size: 42rpx;
					font-weight: 600;
					letter-spacing: 4rpx;
				}
			}

			.gif-wave {
				position: absolute;
				width: 100%;
				bottom: -2rpx;
				left: 0;
				// z-index: 99;
				mix-blend-mode: screen;
				height: 200rpx;
				width: 100%;

				// background-color: #000000;
				.gif-wave-img {
					height: 200rpx;
					width: 100%;
					z-index: 1;
				}
			}
		}
	}

	::v-deep .uni-forms-item__error {
		position: absolute !important;
		top: 80rpx !important;
		left: 22rpx !important;
	}
	.dateRange {
		::v-deep {
			.uni-icons {
				display: none;
			}
		}
	}
	.tip {
		width: calc(100% - 60rpx);
		margin: 0 30rpx 20px;
		font-size: 24rpx;
		color: #9B9B9B;
		text-align: right;
		.desc {
			padding-right: 15rpx;
		}
		.validity {
			width: 100%;
			height: 40rpx;
			padding-bottom: 0;
			text-align: right;
			::v-deep {
				.u-radio-group {
					display: flex;
					justify-content: flex-end;
				}
				.uni-forms-item__error {
					top: 40rpx !important;
					left: auto;
					right: 20rpx;
				}
			}
		}
	}
</style>